/**
Defines the main function for the echo client.
*/
#include "globals.h"

/**
The main function.
*/
int 
main(
    int argc,
    char *argv[]
    )
{
    int sockfd;
    struct sockaddr_in serv_addr;
    int pfd;

    //
    // Create the socket and connect to the server.
    //

    sockfd = Socket(AF_INET, SOCK_STREAM, 0);
    pfd = atoi(argv[2]);

    bzero(&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT_NUM_ECHO); 
    
    Inet_pton(AF_INET, argv[1], &serv_addr.sin_addr);

    if (connect(sockfd, (SA *) &serv_addr, sizeof(serv_addr)) < 0)
    {
        Write(pfd, DIAG_ECHO_CLIENT_CONN_ERR,\
            strlen(DIAG_ECHO_CLIENT_CONN_ERR));

        err_sys("socket error");
    }
    
    //
    // Connection established.
    //

    Write(pfd, DIAG_ECHO_CLIENT_CONN, strlen(DIAG_ECHO_CLIENT_CONN));

    //
    // Ignore SIGPIPE. We will handle this when write returns an error.
    //

    Signal(SIGPIPE, SIG_IGN);

    str_cli_ext(stdin, sockfd, pfd);

    return 0;
}

